<template>
  <el-dialog title="下船" :close-on-click-modal="false" :visible.sync="visible">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="下船时间" prop="sailingTime">
        <el-date-picker
          v-model="dataForm.sailingTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      menuList: [],
      menuListTreeProps: {
        label: "name",
        children: "children"
      },
      // dataForm: {
      //   dispatchId: 0,
      //   sailingTime: ""
      // },
      dataForm: {
        sailingTime: "",
        dispatchId: 0,
        dispatchCrew: "",
        shipownerName: "",
        vesselName: "",
        aboardPost: [],
        aboardTime: "",
        estimateBoardTime: "",
        estimateDisembarkationTime: "",
        dispatchStatus: "",
        shipType: "",
        navigationArea: "",
        deadweightTonnage: "",
        ashoreSite: "",
        remarks: "",
        salaryEquirement: "",
        currency: "",
        contractId: "",
        affiliatedCompanyId: "",
        createUserId: "",
        createTime: ""
      },
      dataRule: {
        sailingTime: [
          { required: true, message: "下船时间不能为空", trigger: "blur" }
        ],
      },
      tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
    };
  },
  methods: {
    init(id) {
      this.dataForm.dispatchId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.dispatchId) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/dispatchinfo/info/${this.dataForm.dispatchId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            console.log(data)
            if (data && data.code === 0) {
              this.dataForm.sailingTime = data.dispatchInfo.sailingTime;
              this.dataForm.dispatchCrew = data.dispatchInfo.dispatchCrew,
              this.dataForm.shipownerName = data.dispatchInfo.shipownerName,
              this.dataForm.vesselName = data.dispatchInfo.vesselName,
              this.dataForm.aboardPost = data.dispatchInfo.aboardPost,
              this.dataForm.aboardTime = data.dispatchInfo.aboardTime,
              this.dataForm.estimateBoardTime = data.dispatchInfo.estimateBoardTime,
              this.dataForm.estimateDisembarkationTime = data.dispatchInfo.estimateDisembarkationTime,
              this.dataForm.dispatchStatus = data.dispatchInfo.dispatchStatus,
              this.dataForm.shipType = data.dispatchInfo.shipType,
              this.dataForm.navigationArea = data.dispatchInfo.navigationArea,
              this.dataForm.deadweightTonnage = data.dispatchInfo.deadweightTonnage,
              this.dataForm.ashoreSite = data.dispatchInfo.ashoreSite,
              this.dataForm.remarks = data.dispatchInfo.remarks,
              this.dataForm.salaryEquirement = data.dispatchInfo.salaryEquirement,
              this.dataForm.currency = data.dispatchInfo.currency,
              this.dataForm.contractId = data.dispatchInfo.contractId,
              this.dataForm.affiliatedCompanyId = data.dispatchInfo.affiliatedCompanyId,
              this.dataForm.createUserId = data.dispatchInfo.createUserId,
              this.dataForm.createTime = data.dispatchInfo.createTime
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/dispatchinfo/updateSailingTime`
            ),
            method: "post",
            data: this.$http.adornData({
              dispatchId: this.dataForm.dispatchId || undefined,
              sailingTime: this.dataForm.sailingTime,
              // remark: this.dataForm.remark,
              // menuIdList: [].concat(
              //   this.$refs.menuListTree.getCheckedKeys(),
              //   [this.tempKey],
              //   this.$refs.menuListTree.getHalfCheckedKeys()
              // )
              dispatchCrew: this.dataForm.dispatchCrew,
              shipownerName: this.dataForm.shipownerName,
              vesselName: this.dataForm.vesselName,
              aboardPost: this.dataForm.aboardPost,
              aboardTime: this.dataForm.aboardTime,
              estimateBoardTime: this.dataForm.estimateBoardTime,
              estimateDisembarkationTime: this.dataForm.estimateDisembarkationTime,
              dispatchStatus: this.dataForm.dispatchStatus,
              shipType: this.dataForm.shipType,
              navigationArea: this.dataForm.navigationArea,
              deadweightTonnage: this.dataForm.deadweightTonnage,
              ashoreSite: this.dataForm.ashoreSite,
              remarks: this.dataForm.remarks,
              salaryEquirement: this.dataForm.salaryEquirement,
              currency: this.dataForm.currency,
              contractId: this.dataForm.contractId,
              affiliatedCompanyId: this.dataForm.affiliatedCompanyId,
              createUserId: this.dataForm.createUserId,
              createTime: this.dataForm.createTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
